package lmh.example.exam1_12;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.SurfaceView;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

public class MainActivity extends Activity
{
    public static final int CAPTURE_CODE = 0x123;
    private MediaProjectionManager projectionManager;
    private int screenDensity;
    private int displayWidth = 1200;
    private int displayHeight = 1800;
    private boolean screenSharing;
    private MediaProjection mediaProjection;
    private VirtualDisplay virtualDisplay;
    private Switch sw;
    private Surface surface;
    private SurfaceView surfaceView;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取屏幕分辨率
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenDensity = metrics.densityDpi;

        sw = findViewById(R.id.sw);
        // 获取应用界面上的SurfaceView组件
        surfaceView = findViewById(R.id.surface);
        surface = surfaceView.getHolder().getSurface();
        // 控制界面上的SurfaceView组件的宽度和高度
        surfaceView.setLayoutParams(new LinearLayout.LayoutParams(displayWidth,displayHeight));
        // 获取MediaProjectionManager管理器
        projectionManager = (MediaProjectionManager) getSystemService(
                Context.MEDIA_PROJECTION_SERVICE); // ①
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    shareScreen();          //自定义方法，开启屏幕捕捉
                }
                else
                {
                    stopScreenSharing();    //自定义方法，关闭屏幕捕捉
                }
            }
        });
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void shareScreen()          //自定义方法，开启屏幕捕捉
    {
        screenSharing = true;
        if (surface == null) {
            return;
        }
        if (mediaProjection == null) {
            Intent intent = projectionManager.createScreenCaptureIntent();  // ②
            startActivityForResult(intent, CAPTURE_CODE);  // ③
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void stopScreenSharing()        //自定义方法，关闭屏幕捕捉
    {
        screenSharing = false;
        if (virtualDisplay == null) {
            return;
        }
        virtualDisplay.release();
        mediaProjection = null;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == CAPTURE_CODE) {
            // 如果resultCode不等于RESULT_OK，表明用户拒绝了屏幕捕捉
            if (resultCode != Activity.RESULT_OK) {
                Toast.makeText(this, "用户取消了屏幕捕捉",
                        Toast.LENGTH_SHORT).show();
                sw.setChecked(false);
                return;
            }
            mediaProjection = projectionManager.getMediaProjection(resultCode, data);  // ④
            virtualDisplay = mediaProjection.createVirtualDisplay("屏幕捕捉",
                    displayWidth, displayHeight, screenDensity,
                    DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                    surface, null /* Callback */, null /*Handler*/);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (mediaProjection != null) {
            mediaProjection.stop();
            mediaProjection = null;
        }
    }
}
